${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}

<#assign classbody>
    <#assign declarationName = pojo.importType(pojo.getDeclarationName())>/**
* Home object for domain model class ${declarationName}.
* @see ${pojo.getQualifiedDeclarationName()}
* @author Keerthi Ramachandran
*/

public class ${declarationName}Dao {

private static final ${pojo.importType("org.apache.commons.logging.Log")} LOGGER = ${pojo.importType("org.apache.commons.LOGGERging.LogFactory")}.getLog(${pojo.getDeclarationName()}Home.class);
    <#if ejb3>

    </#if>
    <#if ejb3>
    @${pojo.importType("javax.persistence.PersistenceContext")} private ${pojo.importType("javax.persistence.EntityManager")} entityManager;

    public void save(${declarationName} transientInstance) {
    LOGGER.debug("persisting ${declarationName} instance");
    try {
    entityManager.persist(transientInstance);
    LOGGER.debug("persist successful");
    }
    catch (RuntimeException re) {
    LOGGER.error("persist failed", re);
    throw re;
    }
    }

    public void remove(${declarationName} persistentInstance) {
    LOGGER.debug("removing ${declarationName} instance");
    try {
    entityManager.remove(persistentInstance);
    LOGGER.debug("remove successful");
    }
    catch (RuntimeException re) {
    LOGGER.error("remove failed", re);
    throw re;
    }
    }

    public ${declarationName} merge(${declarationName} detachedInstance) {
    LOGGER.debug("merging ${declarationName} instance");
    try {
    ${declarationName} result = entityManager.merge(detachedInstance);
    LOGGER.debug("merge successful");
    return result;
    }
    catch (RuntimeException re) {
    LOGGER.error("merge failed", re);
    throw re;
    }
    }

        <#if clazz.identifierProperty?has_content>
        public ${declarationName} findById( ${pojo.getJavaTypeName(clazz.identifierProperty, jdk5)} id) {
        LOGGER.debug("getting ${declarationName} instance with id: " + id);
        try {
        ${declarationName} instance = entityManager.find(${pojo.getDeclarationName()}.class, id);
        LOGGER.debug("get successful");
        return instance;
        }
        catch (RuntimeException re) {
        LOGGER.error("get failed", re);
        throw re;
        }
        }
        </#if>
    <#else>
    private final ${pojo.importType("org.hibernate.SessionFactory")} sessionFactory = ${pojo.importType("com.kee.example.util.HibernateUtil")}..getSessionFactory();



    public void save(${declarationName} transientInstance) {
    LOGGER.debug("persisting ${declarationName} instance");
    try {
    sessionFactory.getCurrentSession().persist(transientInstance);
    LOGGER.debug("persist successful");
    }
    catch (RuntimeException re) {
    LOGGER.error("persist failed", re);
    throw re;
    }
    }

    public void saveOrUpdate(${declarationName} instance) {
    LOGGER.debug("attaching dirty ${declarationName} instance");
    try {
    sessionFactory.getCurrentSession().saveOrUpdate(instance);
    LOGGER.debug("attach successful");
    }
    catch (RuntimeException re) {
    LOGGER.error("attach failed", re);
    throw re;
    }
    }

    public void saveWithLock(${declarationName} instance) {
    LOGGER.debug("attaching clean ${declarationName} instance");
    try {
    sessionFactory.getCurrentSession().lock(instance, ${pojo.importType("org.hibernate.LockMode")}.NONE);
    LOGGER.debug("attach successful");
    }
    catch (RuntimeException re) {
    LOGGER.error("attach failed", re);
    throw re;
    }
    }

    public void delete(${declarationName} persistentInstance) {
    LOGGER.debug("deleting ${declarationName} instance");
    try {
    sessionFactory.getCurrentSession().delete(persistentInstance);
    LOGGER.debug("delete successful");
    }
    catch (RuntimeException re) {
    LOGGER.error("delete failed", re);
    throw re;
    }
    }

    public ${declarationName} merge(${declarationName} detachedInstance) {
    LOGGER.debug("merging ${declarationName} instance");
    try {
    ${declarationName} result = (${declarationName}) sessionFactory.getCurrentSession()
    .merge(detachedInstance);
    LOGGER.debug("merge successful");
    return result;
    }
    catch (RuntimeException re) {
    LOGGER.error("merge failed", re);
    throw re;
    }
    }

        <#if clazz.identifierProperty?has_content>
        public ${declarationName} findById( ${c2j.getJavaTypeName(clazz.identifierProperty, jdk5)} id) {
        LOGGER.debug("getting ${declarationName} instance with id: " + id);
        try {
        ${declarationName} instance = (${declarationName}) sessionFactory.getCurrentSession()
        .get("${clazz.entityName}", id);
        if (instance==null) {
        LOGGER.debug("get successful, no instance found");
        }
        else {
        LOGGER.debug("get successful, instance found");
        }
        return instance;
        }
        catch (RuntimeException re) {
        LOGGER.error("get failed", re);
        throw re;
        }
        }
        </#if>

        <#if clazz.hasNaturalId()>
        public ${declarationName} findByNaturalId(${c2j.asNaturalIdParameterList(clazz)}) {
        LOGGER.debug("getting ${declarationName} instance by natural id");
        try {
        ${declarationName} instance = (${declarationName}) sessionFactory.getCurrentSession()
        .createCriteria("${clazz.entityName}")
            <#if jdk5>
            .add( ${pojo.staticImport("org.hibernate.criterion.Restrictions", "naturalId")}()
            <#else>
            .add( ${pojo.importType("org.hibernate.criterion.Restrictions")}.naturalId()
            </#if>
            <#foreach property in pojo.getAllPropertiesIterator()>
                <#if property.isNaturalIdentifier()>
                .set("${property.name}", ${property.name})
                </#if>
            </#foreach>
        )
        .uniqueResult();
        if (instance==null) {
        LOGGER.debug("get successful, no instance found");
        }
        else {
        LOGGER.debug("get successful, instance found");
        }
        return instance;
        }
        catch (RuntimeException re) {
        LOGGER.error("query failed", re);
        throw re;
        }
        }
        </#if>
        <#if jdk5>
        public ${pojo.importType("java.util.List")}<${declarationName}> findByExample(${declarationName} instance) {
        <#else>
        public ${pojo.importType("java.util.List")} findByExample(${declarationName} instance) {
        </#if>
    LOGGER.debug("finding ${declarationName} instance by example");
    try {
        <#if jdk5>
        ${pojo.importType("java.util.List")}<${declarationName}> results = (List<${declarationName}>) sessionFactory.getCurrentSession()
        <#else>
        ${pojo.importType("java.util.List")} results = sessionFactory.getCurrentSession()
        </#if>
    .createCriteria("${clazz.entityName}")
        <#if jdk5>
        .add( ${pojo.staticImport("org.hibernate.criterion.Example", "create")}(instance) )
        <#else>
        .add(${pojo.importType("org.hibernate.criterion.Example")}.create(instance))
        </#if>
    .list();
    LOGGER.debug("find by example successful, result size: " + results.size());
    return results;
    }
    catch (RuntimeException re) {
    LOGGER.error("find by example failed", re);
    throw re;
    }
    }
        <#foreach queryName in cfg.namedQueries.keySet()>
            <#if queryName.startsWith(clazz.entityName + ".")>
                <#assign methname = c2j.unqualify(queryName)>
                <#assign params = cfg.namedQueries.get(queryName).parameterTypes><#assign argList = c2j.asFinderArgumentList(params, pojo)>
                <#if jdk5 && methname.startsWith("find")>
                public ${pojo.importType("java.util.List")}<${declarationName}> ${methname}(${argList}) {
                <#elseif methname.startsWith("count")>
                public int ${methname}(${argList}) {
                <#else>
                public ${pojo.importType("java.util.List")} ${methname}(${argList}) {
                </#if>
            ${pojo.importType("org.hibernate.Query")} query = sessionFactory.getCurrentSession()
            .getNamedQuery("${queryName}");
                <#foreach param in params.keySet()>
                    <#if param.equals("maxResults")>
                    query.setMaxResults(maxResults);
                    <#elseif param.equals("firstResult")>
                    query.setFirstResult(firstResult);
                    <#else>
                    query.setParameter("${param}", ${param});
                    </#if>
                </#foreach>
                <#if jdk5 && methname.startsWith("find")>
                return (List<${declarationName}>) query.list();
                <#elseif methname.startsWith("count")>
                return ( (Integer) query.uniqueResult() ).intValue();
                <#else>
                return query.list();
                </#if>
            }
            </#if>
        </#foreach></#if>
}
</#assign>

${pojo.generateImports()}
${classbody}
